package com.ph.irp.base.log.action;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.log.service.LogService;
import com.ph.irp.dto.LogDTO;
import com.ph.irp.dto.PageDTO;
import com.ph.util.DateUtil;
import com.ph.util.pagination.PageUtil;
import com.ph.util.pagination.ShowPageList;

public class LogAction extends ActionSupport implements ShowPageList {
	private static int PAGE_SIZE = 10;
	private String FORWARD = null;
	private List<LogDTO> logList;
	private List<LogDTO> tempLogList;
	private LogService logService;

	private String customerName;
	private String timeFrom;
	private String timeTo;
	private PageDTO page;
	private String cpage;

	// 下载文件原始存放路径
	// private final static String DOWNLOADFILEPATH = "/download/";
	private final static String DOWNLOADFILENAME = "PHiRP上机日志";

	public String getCpage() {
		return cpage;
	}

	public void setCpage(String cpage) {
		this.cpage = cpage;
	}

	public PageDTO getPage() {
		return page;
	}

	public void setPage(PageDTO page) {
		this.page = page;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public String getTimeFrom() {
		return timeFrom;
	}

	public void setTimeFrom(String timeFrom) {
		this.timeFrom = timeFrom;
	}

	public String getTimeTo() {
		return timeTo;
	}

	public void setTimeTo(String timeTo) {
		this.timeTo = timeTo;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public List<LogDTO> getLogList() {
		return logList;
	}

	public void setLogList(List<LogDTO> logList) {
		this.logList = logList;
	}

	public String execute() throws Exception {
		try {
			tempLogList = logService.getAllLog();

			if (tempLogList != null) {
				page = PageUtil.generatePageDTO(tempLogList,
						"showLogPageList.action", cpage);
				logList = page.getResList();

				FORWARD = "success";
			} else {
				FORWARD = "input";
			}
		} catch (Exception e) {
			e.printStackTrace();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return FORWARD;
	}

	public String getLogByCondition() throws Throwable {
		tempLogList = logService.getLogByUserNameAndTime(customerName,
				timeFrom, timeTo);

		if (tempLogList != null) {
			if (tempLogList.size() != 0) {
				cpage = "1";
				page = PageUtil.generatePageDTO(tempLogList,
						"showLogPageList.action", cpage);
				logList = page.getResList();
			} else {
				logList = tempLogList;
			}
			FORWARD = "success";
		} else {
			FORWARD = "input";
		}

		return FORWARD;
	}

	public String showPageList() throws Throwable {
		if (tempLogList != null) {
			page = PageUtil.generatePageDTO(tempLogList,
					"showLogPageList.action", cpage);
			logList = page.getResList();
			FORWARD = "success";
		} else {
			FORWARD = "input";
		}
		return FORWARD;
	}

	public String deleteLog() throws Throwable {
		logService.deleteAllLog();
		FORWARD = "success";

		return FORWARD;
	}

	// 从下载文件原始存放路径读取得到文件输出流
	public InputStream getExcelFile() throws Throwable {
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet(DOWNLOADFILENAME);

		createSheetHead(sheet);
		createSheetContent(sheet, logService.getAllLog());

		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		try {
			workbook.write(baos);// 写入
		} catch (IOException e) {
			e.printStackTrace();
		}

		byte[] ba = baos.toByteArray();
		ByteArrayInputStream bais = new ByteArrayInputStream(ba);
		return bais;
	}

	private void createSheetContent(HSSFSheet sheet, List<LogDTO> list) {
		HSSFRow row;
		HSSFCell cell;
		for (int i = 0; i < list.size(); i++) {
			LogDTO log = (LogDTO) list.get(i);

			row = sheet.createRow(i + 1);// 创建第i+1行

			cell = row.createCell(0);// 创建第1列
			cell.setCellValue(new HSSFRichTextString(log.getLogDate()));

			cell = row.createCell(1);// 创建第2列
			cell.setCellValue(new HSSFRichTextString(log.getUserName()));

			cell = row.createCell(2);// 创建第3列
			cell.setCellValue(new HSSFRichTextString(log.getModuleName()));

			cell = row.createCell(3);// 创建第4列
			cell.setCellValue(new HSSFRichTextString(log.getState()));

			// cell = row.createCell(4);// 创建第5列
			// cell.setCellValue(new HSSFRichTextString(log.getLogInfo()));
		}
	}

	private void createSheetHead(HSSFSheet sheet) {
		HSSFRow row = sheet.createRow(0);// 创建第一行

		HSSFCell cell = row.createCell(0); // 创建第一列
		cell.setCellValue(new HSSFRichTextString("日期/时间"));

		cell = row.createCell(1);// 创建第二列
		cell.setCellValue(new HSSFRichTextString("用户名称"));

		cell = row.createCell(2);// 创建第三列
		cell.setCellValue(new HSSFRichTextString("操作模块"));

		cell = row.createCell(3);// 创建第四列
		cell.setCellValue(new HSSFRichTextString("状态"));

		// cell = row.createCell(4);// 创建第五列
		// cell.setCellValue(new HSSFRichTextString("内容描述"));
	}

	// 如果下载文件名为中文，进行字符编码转换
	public String getDownloadChineseFileName() throws Throwable {
		StringBuilder sb = new StringBuilder();
		sb.append(DOWNLOADFILENAME).append("-");
		sb.append(DateUtil.getCurrentDateString());

		String downloadChineseFileName = null;
		try {
			downloadChineseFileName = new String(sb.toString().getBytes(),
					"ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return downloadChineseFileName;
	}

	public String downloadLog() {
		return SUCCESS;
	}

}